/**
 Copyright (C) 2013 Yuyao Liang

 This source code is release under the BSD License.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.

 Created on Nov 12, 2013 2:17:21 PM
 */
package com.googlecode.backtest.document;

import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import com.googlecode.backtest.controller.IBackTestController;
import com.googlecode.backtest.dataset.IBackTestStockDataset;
import com.googlecode.backtest.dataset.provider.BackTestStockDataProvider;
import com.googlecode.backtest.rule.IAlgorithm;
import com.googlecode.backtest.rule.ITAParameters;

/**
 * @author lianyuy
 * 
 */
public class BackTestDocument extends
		AbstractBackTestDocument<IBackTestStockDataset, Item> {

	private IBackTestController<IBackTestStockDataset, ITAParameters, IAlgorithm<IBackTestStockDataset, ITAParameters>> controller;

	/**
	 * @param controller
	 */
	public BackTestDocument(
			IBackTestController<IBackTestStockDataset, ITAParameters, IAlgorithm<IBackTestStockDataset, ITAParameters>> controller) {
		super();
		this.controller = controller;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.googlecode.backtest.document.IBackTestDocument#readTemplate(java.
	 * io.InputStream)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public IBackTestContent<Item> readTemplate(InputStream stream) {
		JAXBContext context;
		BackTestContent<Item> content = null;
		try {
			context = JAXBContext.newInstance(BackTestContent.class,
					Item.class, BackTestStockDataProvider.class);
			Unmarshaller um;
			um = context.createUnmarshaller();
			content = (BackTestContent<Item>) um.unmarshal(stream);
		} catch (JAXBException e1) {
			e1.printStackTrace();
		}
		return content;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.googlecode.backtest.document.IBackTestDocument#writeTemplate(java
	 * .io.OutputStream, com.googlecode.backtest.document.IBackTestContent)
	 */
	@Override
	public void writeTemplate(OutputStream stream,
			IBackTestContent<Item> content) {
		try {
			JAXBContext context = JAXBContext.newInstance(
					BackTestContent.class, Item.class,
					BackTestStockDataProvider.class);
			Marshaller m = context.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			m.marshal(content, stream);
		} catch (JAXBException e1) {
			e1.printStackTrace();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.googlecode.backtest.document.IBackTestDocument#runBackTest(com.googlecode
	 * .backtest.controller.IBackTestController)
	 */
	@Override
	public IBackTestStockDataset runBackTest() {
		if (controller != null) {
			// controller.runAlgorithm(algorithmclass, dataset, parameter);
		}
		return null;
	}
}
